package com.example.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@RefreshScope
@RequestMapping
@Controller
public class IndexController {

    // 引用user.name（正确）
    @Value("${spring.application.name:默认值}")
    private String applicationName;

    // 引用user.name（正确）
    @Value("${user.name:默认值}")
    private String userName;

    @Value("${user.age:默认值}")
    private String userAge;

    // 引用app.version（正确）
    @Value("${app.version:默认值}")
    private String appVersion;

    @RequestMapping("/")
    public String index(Model model) {
        model.addAttribute("time", System.currentTimeMillis());
        return "index";
    }

    @RequestMapping("/id")
    @ResponseBody
    public Object getId() {
        Map<String, Object> map = new HashMap<>();{{
            map.put("userName", userName);
            map.put("userAge", userAge);
            map.put("appVersion", appVersion);
            map.put("time", System.currentTimeMillis());
            map.put("applicationName", applicationName);
        }}
        return map;
    }


}
